﻿using System;
using EzDesk.DesktopClient.Command;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.Model;
using EzDesk.DesktopClient.MVVMEnhance;
using EzDesk.DesktopClient.UtilityView.ViewContract;
using EzDesk.Generic;
using Microsoft.Practices.Unity;

namespace EzDesk.DesktopClient.UtilityView.ViewModel
{
    public interface INotificationItemViewModel:IFeedItemViewModel<INotificationItemView>
    {
        DelegateCommand DisableFeedCommand { get; }
        new INotificationItemViewModel SetModel(Feed feed);
    }

    public class NotificationItemViewModel : 
        FeedItemViewModel<INotificationItemView,INotificationItemViewModel>,  
        INotificationItemViewModel
    {
        [InjectionConstructor]
        public NotificationItemViewModel(INotificationItemView view)
        {
            View = view;
            InitLocalEvent();
        }

        public NotificationItemViewModel()
        {
            InitLocalEvent();
        }

        public DelegateCommand DisableFeedCommand { get; private set; }

        private void InitLocalEvent()
        {
            DisableFeedCommand = new DelegateCommand(OnDisableFeed);
            View.OnNavigate += OnNavigate;
        }

        public new INotificationItemViewModel SetModel(Feed feed)
        {
            base.SetModel(feed);
            return this;
        }

        private void OnDisableFeed()
        {
            this.IsAvailable = false;
            OnFeedStatusChanged();
        }

        private void OnFeedStatusChanged()
        {
            FeedStatusChangeEvent.Instance.Publish(FeedModel);
        }

        private void OnNavigate(object sender, GenericEventArg<string> e)
        {
            FunctionNavigateEvent.Instance.Publish(new FunctionNavigateArgs { Url = e.Value });
        }

        protected override void OnDispose()
        {
            this.DisableFeedCommand = null;
            View.OnNavigate -= OnNavigate;
            base.OnDispose();
        }
    }
}
